import { Component, OnInit } from '@angular/core';
import {Globals} from '../../globals';
import {PageHelper} from '../../utils/page-helper-model';
import {FormBuilder, FormGroup} from '@angular/forms';
import {BaseService} from '../../shared/service/base.service';
import {MyDateModel} from '../../models/my-date.model';
import {ApiUrl} from '../../config/api-url';

@Component({
  selector: 'app-money-details',
  templateUrl: './money-details.component.html',
  styleUrls: ['./money-details.component.less']
})
export class MoneyDetailsComponent implements OnInit {
  isMobile = Globals.isMobile();
  listOfData: any;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  // 模态框表单
  formModel: FormGroup;

  // 开始日期类型
  startDateTimeType: string = 'start';
  // 结束日期类型
  endDateTimeType: string = 'end';
  // 开始日期
  startDateTime: Date;
  // 结束日期
  endDateTime: Date;
  constructor(private baseService: BaseService, private fb: FormBuilder) { }

  ngOnInit(): void {
    this.startDateTime = this.baseService.zorro.getDateTime().startToday;
    this.endDateTime = this.baseService.zorro.getDateTime().endToday;
    this.buildForm();
    this.findData();
  }
  buildForm() {
    this.formModel = this.fb.group({
      type: [null, []],
      cashFlowType: [null, []],
      keyword: [null, []],
      startDate: [null, []],
      endDate: [null, []],
    });
  }
  submitForm() {
    this.pageHelper.currentPage = 1;
    this.findData();
  }


  // 将开始、结束时间往外抛
  outDateTime($event: MyDateModel) {
    /**
     * date: Fri Mar 19 2021 23:59:06 GMT+0800 (中国标准时间) {}
     * type: "end"
     */
    if ($event.type == 'start') {
      this.startDateTime = $event.date;
    } else if ($event.type == 'end') {
      this.endDateTime = $event.date;
    }
  }
  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findData();
  }
  onPageSizeChange(pageSize: any){
    this.pageHelper.pageSize = pageSize;
    this.pageHelper.currentPage = 1;
    this.findData();
  }
  findData(){
    this.formModel.get("startDate").setValue(this.startDateTime);
    this.formModel.get("endDate").setValue(this.endDateTime);
    this.baseService.findListByMuchCondition(ApiUrl.MoneyDetailsUrl + '/findList', this.formModel.value, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfData = data.data.list;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }


}
